--  ---------------------------------------------------------------------------
--  @File: symbol.adb
--
--  @Project: LiberAda
--  @Version: 1.0
--  @Created: 2009-10-08
--  @Author:  Ivan Bogicevic
--
--  @Description:
--    For a detailed description of this package (@see symbol.ads).
--  @Implementation:
--    Mainly simple setter and getter function to provide access to the data.
--
--  @Modifications:
--
--  ---------------------------------------------------------------------------
with File_Tools;

package body Symbol is

   --  Setter-Functions for private data of a symbol

   --  ------------------------------------------------------------------------
   --  @Procedure: Set_Filepath
   procedure Set_Filepath
     (This     : in out Symbol_Type;
      Filepath : in     ASU.Unbounded_String) is
   begin
      This.Filepath := Filepath;
   end Set_Filepath;

   --  ------------------------------------------------------------------------
   --  @Procedure: Set_Packagename
   procedure Set_Packagename
     (This        : in out Symbol_Type;
      Packagename : in     ASU.Unbounded_String) is
   begin
      This.Packagename := Packagename;
   end Set_Packagename;

   --  ------------------------------------------------------------------------
   --  @Procedure: Set_Name
   procedure Set_Name
     (This : in out Symbol_Type;
      Name : in     ASU.Unbounded_String) is
   begin
      This.Name := Name;
   end Set_Name;

   --  ------------------------------------------------------------------------
   --  @Procedure: Add_Parametertype
   procedure Add_Parametertype
     (This          : in out Symbol_Type;
      Parametertype : in     ASU.Unbounded_String) is
      use ASU;
   begin
      This.Parametertypes.Append (Parametertype);
   end Add_Parametertype;

   procedure Extend_Last_Parametertype
     (This      : in out Symbol_Type;
      Extension : in     ASU.Unbounded_String) is
      New_Parametertype : ASU.Unbounded_String;
   begin
      if String_Vector.Has_Element (This.Parametertypes.Last) then
         New_Parametertype := String_Vector.Element
           (This.Parametertypes.Last);
         ASU.Append (New_Parametertype, Extension);
         String_Vector.Replace_Element
           (This.Parametertypes,
            This.Parametertypes.Last, New_Parametertype);
      else
         New_Parametertype := ASU.Null_Unbounded_String;
         ASU.Append (New_Parametertype, Extension);
      end if;
   end Extend_Last_Parametertype;

   --  ------------------------------------------------------------------------
   --  @Procedure: Set_Kind
   procedure Set_Kind
     (This : in out Symbol_Type;
      Kind : in     Symbol_Kind_Type) is
   begin
      This.Kind := Kind;
   end Set_Kind;

   --  ------------------------------------------------------------------------
   --  @Procedure: Clear
   procedure Clear (This : in out Symbol_Type) is
   begin
      This.Filepath       := ASU.To_Unbounded_String ("");
      This.Packagename    := ASU.To_Unbounded_String ("");
      This.Name           := ASU.To_Unbounded_String ("");
      String_Vector.Clear (This.Parametertypes);
      This.Kind           := EMPTY_T;
   end Clear;

   --  Getter-Functions for private data of a symbol

   --  ------------------------------------------------------------------------
   --  @Function: Get_Filepath
   function Get_Filepath (This : in Symbol_Type)
      return ASU.Unbounded_String is
   begin
      return This.Filepath;
   end Get_Filepath;

   --  ------------------------------------------------------------------------
   --  @Function: Get_Packagename
   function Get_Packagename (This : in Symbol_Type)
      return ASU.Unbounded_String is
   begin
      return This.Packagename;
   end Get_Packagename;

   --  ------------------------------------------------------------------------
   --  @Function: Get_Name
   function Get_Name (This : in Symbol_Type) return ASU.Unbounded_String is
   begin
      return This.Name;
   end Get_Name;

   --  ------------------------------------------------------------------------
   --  @Function: Get_Parametertypes
   function Get_Parametertypes (This : in Symbol_Type)
      return String_Vector.Vector is
   begin
      return This.Parametertypes;
   end Get_Parametertypes;

   --  ------------------------------------------------------------------------
   --  @Function: Get_Kind
   function Get_Kind (This : in Symbol_Type) return Symbol_Kind_Type is
   begin
      return This.Kind;
   end Get_Kind;

   --  ------------------------------------------------------------------------
   --  @Function: Get_ID
   function Get_ID (This : in Symbol_Type) return ASU.Unbounded_String is
      ID : ASU.Unbounded_String;
      Strings_Cursor : String_Vector.Cursor;
   begin
      ID := File_Tools.Extract_Filename (This.Filepath, True);
      ASU.Append (ID, ":");
      ASU.Append (ID, This.Packagename);
      ASU.Append (ID, ":");
      ASU.Append (ID, This.Name);
      Strings_Cursor := This.Parametertypes.First;
      while String_Vector.Has_Element (Strings_Cursor) loop
         ASU.Append (ID, ":");
         ASU.Append (ID, String_Vector.Element (Strings_Cursor));
         Strings_Cursor := String_Vector.Next (Strings_Cursor);
      end loop;
      ASU.Append (ID, ":");
      ASU.Append (ID, This.Kind'Img);
      return ID;
   end Get_ID;

   --  ------------------------------------------------------------------------
   --  @Function: Get_Link
   function Get_Link (This : in Symbol_Type) return ASU.Unbounded_String is
      Link : ASU.Unbounded_String;
   begin
      Link := File_Tools.Extract_Filename (This.Filepath, True);
      ASU.Append (Link, ".html#");
      ASU.Append (Link, This.Packagename);
      ASU.Append (Link, ".");
      ASU.Append (Link, This.Name);
      return Link;
   end Get_Link;

   --  ------------------------------------------------------------------------
   --  @Function: "="
   function "=" (Left, Right : Symbol_Type) return Boolean is
      use Ada.Strings.Unbounded;
   begin
      return Left.Get_ID = Right.Get_ID;
   end "=";

end Symbol;
